package cn.cau.epidemicsurveysystem.control;

import cn.cau.epidemicsurveysystem.bean.DiseaseInfoBean;
import cn.cau.epidemicsurveysystem.bean.StaffInfoBean;
import cn.cau.epidemicsurveysystem.service.DiseaseInfoService;
import cn.cau.epidemicsurveysystem.service.TrackService;
import cn.cau.epidemicsurveysystem.vo.TemplateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @projectName: gp2023
 * @package: cn.cau.epidemicsurveysystem.control
 * @className: TrackController
 * @author: zy
 * @description: TODO
 * @date: 2022/3/7 17:12
 * @version: 1.0
 */
@RestController
public class TrackController {
    @Autowired
    TrackService trackService;
    @Autowired
    DiseaseInfoService diseaseInfoService;

    @InitBinder
    public void initBinder(ServletRequestDataBinder binder){
        //只要网页中传来的数据格式为yyyy-MM-dd 就会转化为Date类型
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
    }

    @RequestMapping("/diseaseList")
    public ModelAndView staffList(){
        ModelAndView modelAndView=new ModelAndView();
        List<DiseaseInfoBean> diseaseInfoBeans=diseaseInfoService.findAll();
        modelAndView.addObject("diseaseInfoBeans",diseaseInfoBeans);
        modelAndView.setViewName("epid_diseases");
        return modelAndView;
    }

    @RequestMapping("/trackByDate")
    public ModelAndView getTracks(@RequestParam("start") String start,@RequestParam("end") String end,@RequestParam("diseaseId") int diseaseId){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("track",trackService.getTracksByDiseaseIdAndTypeAndDate(diseaseId,1,start,end));
        modelAndView.addObject("diseaseId",diseaseId);
        modelAndView.setViewName("epidemic_graph");
        return modelAndView;
    }
}
